package lib

import "strconv"

func init() {
	Probs = append(Probs, Problem{
		Num:         43,
		Discription: "字符串形式的大整数相乘",
		Level:       2,
		Labels: map[string]int{
			"字符串":  1,
			"位数操作": 1,
			"大整数":  1,
		},
	})
}

// Multiply 列竖式正常的乘法，记录时高位开始是0，最低位的索引是len(num1)+len(num2)-1
//  @param num1
//  @param num2
//  @return string
func Multiply(num1 string, num2 string) string {
	if num1 == "0" || num2 == "0" {
		return "0"
	}

	//多预留一位，以防最高位进位，先不处理进位
	res := make([]int, len(num1)+len(num2))
	for i := len(num1) - 1; i >= 0; i-- {
		a := int(num1[i] - '0')
		for j := len(num2) - 1; j >= 0; j-- {
			b := int(num2[j] - '0')
			//每一位相乘,注意是+=
			res[i+j+1] += a * b
		}
	}

	//处理进位，最高位单独处理
	str := ""
	for i := len(num1) + len(num2) - 1; i >= 1; i-- {
		res[i-1] += res[i] / 10
		res[i] = res[i] % 10
		str = strconv.Itoa(res[i]) + str
	}

	if res[0] != 0 {
		str = strconv.Itoa(res[0]) + str
	}

	return str
}
